package cc.angis.xiugallery.db;

import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * 数据库操作抽象类
 * 
 * @author piers.xie
 */
public abstract class BaseDBHelper {

	protected DBHelper mDBHelper;
	protected SQLiteDatabase mSQLiteWriter;
	protected SQLiteDatabase mSQLiteReader;
	protected String mTable;
	protected ContentValues mValues;
	protected String mWhereClaus = null;
	protected String mWhereArgs[] = null;
	protected Context mContext;
	protected Cursor mCursor;
	public BaseDBHelper() {
		super();
	}

	public BaseDBHelper(Context context, String table) {
		super();
		mContext = context;
		this.mDBHelper = new DBHelper(context);
		this.mTable = table;
	}

	public BaseDBHelper(Context context, String table, ContentValues values) {
		super();
		this.mDBHelper = new DBHelper(context);
		this.mTable = table;
		this.mValues = values;
	}

	protected DBHelper getDBHelper() {
		return mDBHelper;
	}

	protected String getTable() {
		return mTable;
	}

	protected void setTable(String mTable) {
		this.mTable = mTable;
	}

	protected ContentValues getValues() {
		return mValues;
	}

	protected void setValues(ContentValues mValues) {
		this.mValues = mValues;
	}

	protected String getWhereClaus() {
		return mWhereClaus;
	}

	protected void setWhereClaus(String mWhereClaus) {
		this.mWhereClaus = mWhereClaus;
	}

	protected String[] getWhereArgs() {
		return mWhereArgs;
	}

	protected void setWhereArgs(String[] mWhereArgs) {
		this.mWhereArgs = mWhereArgs;
	}

	/**
	 * 插入一条记录到数据库
	 * 
	 * @return
	 */
	public synchronized long insertDB() {
		if (null == mSQLiteWriter)
			mSQLiteWriter = mDBHelper.getWritableDatabase();
		if (null != mTable && null != mValues) {
			long result = mSQLiteWriter.insert(mTable, null, mValues);
			return result;
		} else {
			return -1;
		}
	}

	/**
	 * 更新一条记录到数据库
	 * 
	 * @return
	 */
	public synchronized long updateDB() {
		if (null == mSQLiteWriter) {
			mSQLiteWriter = mDBHelper.getWritableDatabase();
		}
		if (null != mTable && null != mValues) {
			long result = mSQLiteWriter.update(mTable, mValues, mWhereClaus,
					mWhereArgs);
			return result;
		} else {
			return -1;
		}
	}

	/**
	 * 删除一条记录到数据库
	 * 
	 * @return
	 */
	public synchronized long delDB() {
		if (null == mSQLiteWriter) {
			mSQLiteWriter = mDBHelper.getWritableDatabase();
		}
		if (null != mTable) {
			long result = mSQLiteWriter.delete(mTable, mWhereClaus, mWhereArgs);
			return result;
		} else {
			return -1;
		}
	}

	/**
	 * 删除所有数据库记录
	 * 
	 * @return
	 */
	public synchronized long deleteAll(String username) {
		return delDB();
	}

	/**
	 * 判断是否存在ID的记录
	 * 
	 * @return
	 */
	public synchronized boolean isExist(String column, String value) {
		boolean isExist = false;
		if (null == mSQLiteReader)
			mSQLiteReader = mDBHelper.getReadableDatabase();
		Cursor lCursor = mSQLiteReader.query(mTable, new String[] { column },
				column + "=?", new String[] { value }, null, null, null);
		if (lCursor.moveToFirst()) {
			isExist = true;
		}
		lCursor.close();
		return isExist;
	}
	
	/**
	 * 判断是否存在记录
	 * 
	 * @return
	 */
	public synchronized boolean isExist(String column,String column2,String value, String value2) {
		boolean isExist = false;
		if (null == mSQLiteReader)
			mSQLiteReader = mDBHelper.getReadableDatabase();
		Cursor lCursor = mSQLiteReader.query(mTable, new String[] { column ,column2},
				column + "=? and " + column2 +"=?", new String[] { value,value2 }, null, null, null);
		if (lCursor.moveToFirst()) {
			isExist = true;
		}
		lCursor.close();
		return isExist;
	}

	/**
	 * 关闭数据库
	 */
	public synchronized void closeDB() {
		if (null != mCursor) {
			mCursor.close();
		}
		if (null != mSQLiteWriter) {
			mSQLiteWriter.close();
		}
		if (null != mSQLiteReader) {
			mSQLiteReader.close();
		}
		if(mDBHelper != null)
			mDBHelper.close();
		mDBHelper = null;
	}
	
    public synchronized void beginTransaciton(){
		if (null == mSQLiteWriter) {
			mSQLiteWriter = mDBHelper.getWritableDatabase();
		}
		mSQLiteWriter.beginTransaction();
    }
    
    public synchronized void setTransactionSuccessful(){
		if (null == mSQLiteWriter) {
			mSQLiteWriter = mDBHelper.getWritableDatabase();
		}
		mSQLiteWriter.setTransactionSuccessful();
    }
    
    public synchronized void endTransaction(){
		if (null == mSQLiteWriter) {
			mSQLiteWriter = mDBHelper.getWritableDatabase();
		}
		mSQLiteWriter.endTransaction();
    }
    
    
    public synchronized void execSql(String sql){
		if (null == mSQLiteWriter)
			mSQLiteWriter = mDBHelper.getReadableDatabase();
		mSQLiteWriter.execSQL(sql);
    }
    
	public String getOrWhereSting(String columeName,List<String> selection){
		StringBuilder where = new StringBuilder();
		for(int i = 0;i<selection.size();i++){
			where.append(columeName);
			where.append("='");
			where.append(selection.get(i));
			where.append("'");
			if(i<selection.size()-1)
				where.append(" OR ");
		}
		return where.toString();
	}

}
